package com.example.lib_cache.network.response;

import android.util.Log;

import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_common.util.ReflectUtils;
import com.netease.nim.uikit.common.util.sys.ReflectionUtil;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class ResponseResultTransformer<T> implements ObservableTransformer<IResponse<T>, T> {

    private CompositeDisposable compositeDisposable;

    public ResponseResultTransformer(CompositeDisposable compositeDisposable){
        this.compositeDisposable = compositeDisposable;
    }

    public ResponseResultTransformer(){}

    @Override
    public ObservableSource<T> apply(Observable<IResponse<T>> upstream) {
        return upstream
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        if(compositeDisposable != null){
                            compositeDisposable.add(disposable);
                            Log.d("TAG","我是2");
                        }
                    }
                }).onErrorResumeNext(new Function<Throwable, ObservableSource<? extends IResponse<T>>>() {
                    @Override
                    public ObservableSource<? extends IResponse<T>> apply(Throwable throwable) throws Exception {
                        return Observable.error(ApiException.handleException(throwable));


                    }
                }).flatMap(new Function<IResponse<T>, ObservableSource<? extends T>>() {
                    @Override
                    public ObservableSource<? extends T> apply(IResponse<T> tiResponse) throws Exception {
                        //业务成功请求后，即"msg：success"

                        if(tiResponse.isSuccess()){
                            return Observable.just(tiResponse.getData());
                        }else {
                            try {
//                                Log.d("TAG","我是1");
//                                Log.d("TAG","Data的code"+tiResponse.getCode());
                                //反射创建没有内容的实例，防止NULL传值异常
                                Class<?> cls = Class
                                        .forName("com.example.lib_cache.network.response.IResponse");
                                T obj = (T) cls.newInstance();
                                return Observable.just(obj);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        return Observable.error(new ApiException(tiResponse.getCode(),tiResponse.getMsg()));
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 获取ResponseResultTransformer
     *
     * @param compositeDisposable
     * @param <U>
     * @return
     */
    public static  <U> ResponseResultTransformer<U> obtain(CompositeDisposable compositeDisposable){
        return new ResponseResultTransformer<>(compositeDisposable);
    }

    public static <U> ResponseResultTransformer<U> obtain(){
        return new ResponseResultTransformer<>();
    }
}
